package com.sxj.corejava.code07_面向对象.bms;

import java.util.Scanner;

/**
 * @author 石小俊
 * @date 2025年07月22日 13:59
 * 银行主菜单
 */
public class BankMenu {

    private Scanner sc = new Scanner(System.in);

    private Account account;

    public Account getAccount() {
        return account;
    }

    public void setAccount(Account account) {
        this.account = account;
    }

    public BankMenu(Account account) {
        this.account = account;
    }

    public void showMenu() {
        System.out.println("*********************恭喜" + account.getName() + "成功进入紫金银行*********************");
        System.out.print("请选择服务(1.存款 2.取款 3.查询余额 4.转账 5.贷款 6.还款 7.查询贷款金额 8.修改密码 9.退出登录):");
        // 每一个服务对应一个方法
        int choice = sc.nextInt();
        switch (choice) {
            case 1:
                deposit();
                break;
            case 2:
                withdraw();
                break;
            case 3:
                queryBalance();
                break;
            case 4:
                transfer();
                break;
            case 5:
                providerLoan();
                break;
            case 6:
                payLoan();
                break;
            case 7:
                queryLoan();
                break;
            case 8:
                modifyPassword();
                break;
            case 9:
                logout();
                break;
            default:
                System.out.println("错误:您输入的选项有误,请重新输入");
                showMenu();
                break;
        }
    }

    public void deposit() {
        System.out.println("*****************开始存款*****************");
        System.out.print("请输入存款金额:");
        double money = sc.nextDouble();
        // 调用账户提供的存款的方法
        account.deposit(money);
        System.out.println("提示:恭喜您,存款成功!");
        showMenu();
    }

    public void withdraw() {
        System.out.println("*****************开始取款*****************");
        System.out.print("请输入取款金额:");
        double money = sc.nextDouble();

        // 调用取款方法
        boolean flag = account.withdrawMoney(money);
        if (flag) {
            System.out.println("提示:恭喜您,取款成功!");
        } else {
            System.out.println("错误:对不起,您的账户余额不足,取款失败!");
        }
        showMenu();
    }

    public void queryBalance() {
        System.out.println("*****************查询余额*****************");
        System.out.println("您当前账户余额为:" + account.getBalance() + "元");
        showMenu();
    }

    public void transfer() {
        System.out.println("*****************开始转账*****************");
        System.out.print("请输入需要转入的目标账户的卡号:");
        int id = sc.nextInt();
        // 判断当前卡号是否存在
        Account target = Bank.findById(id);
        if (target == null) {
            System.out.println("错误:目标账户不存在!");
        } else {
            System.out.print("请输入转账金额:");
            double money = sc.nextDouble();
            // 判断当前账户类型
            if (account instanceof CreditAccount) {
                CreditAccount creditAccount = (CreditAccount) account;
                // 如果是信用账户,那么账户余额与信用额度之和不能低于转账金额
                if (creditAccount.getBalance() + creditAccount.getLimit() >= money) {
                    // 当前账户余额减少
                    account.setBalance(account.getBalance() - money);
                    // 目标账户余额增加
                    target.setBalance(target.getBalance() + money);
                    System.out.println("提示:恭喜您,转账成功!");
                } else {
                    System.out.println("错误:对不起,您的账户余额不足,转账失败");
                }
            } else {
                // 如果是储蓄账户,那么账户余额不能低于转账金额
                if (account.getBalance() >= money) {
                    // 当前账户余额减少
                    account.setBalance(account.getBalance() - money);
                    // 目标账户余额增加
                    target.setBalance(target.getBalance() + money);
                    System.out.println("提示:恭喜您,转账成功!");
                } else {
                    System.out.println("错误:对不起,您的账户余额不足,转账失败");
                }
            }

        }
        showMenu();
    }

    public void providerLoan() {
        System.out.println("*****************开始贷款*****************");
        // 只有贷款类账户才能操作贷款相关功能
        // 需要判断当前账户是否属于贷款类型账户
//        Loanable loanable = new LoanSavingAccount();
//        Loanable loanable2 = new LoanCreditAccount();
        if (account instanceof Loanable) {
            Loanable loanable = (Loanable) account;
            System.out.print("请输入您需要贷款的金额:");
            double money = sc.nextDouble();
            // 调用贷款方法
            loanable.providerLoan(money);
            System.out.println("提示:恭喜您,贷款成功!");
        } else {
            System.out.println("错误:对不起,您的账户不是贷款类账户,无法操作贷款功能");
        }
        showMenu();
    }

    public void payLoan() {
        System.out.println("*****************开始还贷*****************");
        if (account instanceof Loanable) {
            Loanable loanable = (Loanable) account;
            System.out.print("请输入您需要还贷的金额:");
            double money = sc.nextDouble();
            // 调用还贷方法
            boolean flag = loanable.payLoan(money);
            if (flag) {
                System.out.println("提示:恭喜您,还贷成功!");
            } else {
                System.out.println("错误:对不起,您的账户余额不足,还贷失败!");
            }
        } else {
            System.out.println("错误:对不起,您的账户不是贷款类账户,无法操作贷款功能");
        }
        showMenu();
    }

    public void queryLoan() {
        System.out.println("*****************查询贷款金额*****************");
        if (account instanceof Loanable) {
            Loanable loanable = (Loanable) account;
            System.out.println("提示:您当前贷款总额为:" + loanable.queryLoan());
        } else {
            System.out.println("错误:对不起,您的账户不是贷款类账户,无法操作贷款功能");
        }
        showMenu();
    }

    public void modifyPassword() {
        System.out.println("*****************修改密码*****************");
        System.out.print("请输入原密码:");
        String oldPassword = sc.next();
        // 只有原密码正确才能执行修改密码的功能
        // 判断原密码是否正确
        if (oldPassword.equals(account.getPassword())) {
            // 如果原密码正确,则执行修改密码的操作
            System.out.print("请输入新密码:");
            String newPassword = sc.next();
            System.out.print("请再次输入新密码:");
            String newPassword2 = sc.next();
            if (newPassword.equals(newPassword2)) {
                account.setPassword(newPassword);
                System.out.println("提示:恭喜您,密码修改成功,请重新登录");
                new Start().login();
            } else {
                System.out.println("错误:两次输入的密码不一致!");
            }
        } else {
            System.out.println("错误:您输入的原密码有误!");
        }
        showMenu();
    }

    public void logout() {
        System.out.println("提示:您已经成功退出!");
        new Start().showMenu();
    }
}
